/*
 * This file is part of aion-unique <aionunique.smfnew.com>.
 *
 *  aion-unique is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  aion-unique is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with aion-unique.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.network.aion.serverpackets;

import com.aionengine.gameserver.model.DialogPage;
import com.aionengine.gameserver.model.gameobjects.AionObject;
import com.aionengine.gameserver.model.gameobjects.Npc;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.network.aion.AionConnection;
import com.aionengine.gameserver.network.aion.AionServerPacket;
import com.aionengine.gameserver.services.player.PlayerMailboxState;
import com.aionengine.gameserver.world.MapRegion;
import com.aionengine.gameserver.world.World;
import com.aionengine.gameserver.world.zone.ZoneInstance;

import java.util.List;

/**
 * @author alexa026
 */
public class SM_DIALOG_WINDOW extends AionServerPacket {

    private int targetObjectId;
    private int dialogID;
    private int questId = 0;

    public SM_DIALOG_WINDOW(int targetObjectId, int dlgID) {
        this.targetObjectId = targetObjectId;
        this.dialogID = dlgID;
    }

    public SM_DIALOG_WINDOW(int targetObjectId, int dlgID, int questId) {
        this.targetObjectId = targetObjectId;
        this.dialogID = dlgID;
        this.questId = questId;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void writeImpl(AionConnection con) {
        Player player = con.getActivePlayer();

        writeD(targetObjectId);
        writeH(dialogID);
        writeD(questId);
        writeH(0);
        if (this.dialogID == DialogPage.MAIL.id()) {
            AionObject object = World.getInstance().findVisibleObject(targetObjectId);
            if (object != null && object instanceof Npc) {
                Npc znpc = (Npc) object;
                if (znpc.getNpcId() == 798100 || znpc.getNpcId() == 798101) {
                    player.getMailbox().mailBoxState = PlayerMailboxState.EXPRESS;
                    writeH(2);
                } else
                    player.getMailbox().mailBoxState = PlayerMailboxState.REGULAR;
            } else
                writeH(0);
        } else if (this.dialogID == DialogPage.TOWN_CHALLENGE_TASK.id()) {
            AionObject object = World.getInstance().findVisibleObject(targetObjectId);
            if (object != null && object instanceof Npc) {
                Npc npc = (Npc) object;
                if (npc.getNpcId() == 205770 || npc.getNpcId() == 730677 || npc.getNpcId() == 730679) {
                    int townId = 0;
                    MapRegion region = npc.getPosition().getMapRegion();
                    if (region == null) {
                        // some npc without region !!!
                    } else {
                        List<ZoneInstance> zones = region.getZones(npc);
                        for (ZoneInstance zone : zones) {
                            townId = zone.getTownId();
                            if (townId > 0)
                                break;
                        }
                        writeH(townId);
                    }
                }
            }
        } else
            writeH(0);
    }

}
